package pl.diagnoser.server.persistance.map;

import java.io.Serializable;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import pl.diagnoser.client.dto.EstimateDTO;

@Entity
@Table(name = "T_ESTIMATE")
public class Estimate implements Serializable {

        private static final long serialVersionUID = 1L;
        private long id;
        private Date estDate;
        private String defect;
        private Objects estimateObject;
        
        public Estimate() {}
        public Estimate( Date estDate, String defect, Objects objects ) {
                this.estDate = estDate;
                this.defect = defect;
                this.estimateObject = objects;
        }
        
        public Estimate( EstimateDTO estimate ) {
                SimpleDateFormat formatter = new SimpleDateFormat("d/M/yyyy");
                
                this.defect = estimate.getDefect();
                try {
                        this.estDate = formatter.parse(estimate.getEstDate());
                } catch (ParseException e) {
                        e.printStackTrace();
                }
        }
        
        @Id
        @GeneratedValue( strategy = GenerationType.IDENTITY )
        @Column( name="EST_ID", unique = true, nullable = false )
        public long getId() {
                return this.id;
        }
        
        public void setId( long id ) {
                this.id = id;
        }
        
        @Column( name="EST_DATE", unique = false, nullable = false )
        public Date getEstDate() {
                return estDate;
        }
        
        public void setEstDate( Date estDate ) {
                this.estDate = estDate;
        }
        
        @Column( name="EST_RESULT", unique = false, nullable = true )
        public String getDefect() {
                return defect;
        }
        
        public void setDefect( String defect ) {
                this.defect = defect;
        }
        
        @ManyToOne
        @JoinColumn(name="EST_OBJ_ID")
        public Objects getEstimateObject() {
                return estimateObject;
        }
        
        public void setEstimateObject(Objects object) {
                this.estimateObject = object;
        }
        
        public String toString()
        {
                String ret = "<estimate><estimateId>" + this.getId() + "</estimateId>";
                ret += "<estimateDate>" + this.getEstDate() + "</estimateDate>";
                ret += "<estimateDefect>" + this.getDefect() + "</estimateDefect>";
                
                return ret;
        }
}